<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>HTML5其他功能</title>
<style>
	#t1{ width:400px; height:100px; border:1px #000 solid; }
</style>
</head>
	<body>
	 	<h1>聊天室</h1>
	 	<textarea id="t1"></textarea>
 	<br>
	 	<input type="text"><input type="button" value="留言">
	</body>
	<script>
var oT = document.getElementById('t1');
var aInput = document.getElementsByTagName('input');
	var ws = new WebSocket('ws://localhost:3000');
 	ws.onopen = function(){
	console.log('链接ws');
 	};
	ws.onmessage = function(ev){
 		oT.value += ev.data + '\n';	
	};
	aInput[1].onclick = function(){	
	ws.send(aInput[0].value);
};
	</script>
	<script>
	/* 服务端JS */
	var wss = new WebSocket.Server({ server });
	var personObj = {};
	var i = 0;
	wss.on('connection',function(ws){
		console.log('客户端握手webSocket');
		ws.name = i++;
	personObj[ws.name] = ws;
		ws.on('message',function(data){
			broadcast(ws,data);
	});
 	ws.on('close',function(){  
			delete personObj[ws.name];
	});
	});
	function broadcast(ws,data){
 		for(var attr in personObj){
			personObj[attr].send(ws.name + '说：' + data);
	}
	}
	</script>

